<?php

namespace App\Services\WithdrawByAlipay;

use Carbon\Carbon;
use App\Models\Withdraw;
use App\Models\GoldRound;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Log;
use App\Services\ApplyGoldCoinWithdraw\ApproveGoldCoinWithdraw;

class WithdrawByAlipay
{
	protected function withdraw()
	{
		Withdraw::query()
			->where('method', 'alipay')
			->where('status', 'pending')
			->whereDate('created_at', Carbon::yesterday())
			->chunks(50, function (Collection $withdraws) {

				if ($withdraws->isEmpty()) return false;

				foreach ($withdraws as $withdraw) {
                    app(ApproveGoldCoinWithdraw::class, ['withdraw' => $withdraw])->approve();
				}

				return true;
			});
	}

	public function __invoke()
	{
        if (GoldRound::sIsTodayStormOff()) return;

		Log::debug("========== Start withdraw ......");

		$this->withdraw();

		Log::debug("========== Withdraw paid ......");
	}
}
